package com.itheima.autoconfig;

import com.baidu.aip.face.AipFace;
import com.baidu.aip.util.Base64Util;
import lombok.Data;
import org.json.JSONObject;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Data
@Component
@ConfigurationProperties("tanhua.aip")
public class AipFaceTemplate {

    private String appId;
    private String apiKey;
    private String secretKey;

    public boolean detect(byte[] body){
        AipFace client = new AipFace(appId, apiKey, secretKey);
        //传入可选梣属调用接口
        HashMap<String, String> options = new HashMap<>();
        options.put("face_field","age");
        options.put("max_face_num","2");
        options.put("face_type","LIVE");
        options.put("liveness_control","NONE");

        String imageType="BASE64";

        String image = Base64Util.encode(body);
        //人类检测
        JSONObject res = client.detect(image, imageType, options);
        int code = res.getInt("error_code");
        return code==0;


    }

}